<?php

namespace App\Models;

class Deputy extends Base
{
    protected $table = 'deputys';

    const STATUS_1 = 1;
    const STATUS_2 = 2;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">锁定</span>',
    ];

    protected $fillable = [
        'id','thumb','status','cid','name'
    ];

    public $timestamps = false;//关闭自动维护

    public function category(){
        return $this->belongsTo('App\Models\Category', 'cid', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*']){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['cid']) && $params['cid']) {
                    $query ->where('cid',$params['cid']);
                }
            })->with(['category' => function ($query) {
                $query->select(['id','name'])->get();
            }])->orderBy('id', 'desc')
            ->paginate(config('app.limit'));
        $this->stateToText($lists,[
            'status' => self::STATUS_TEXT,
        ]);
        return $lists;
    }

}
